﻿using System;
using System.Web;
using System.Web.UI;
using Cms.Utilities;

namespace Cms.Web.UI.Admin
{
    public partial class ProcessUpgradeStep : Page
    {
        private IUpdater _updater;

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.Now.AddSeconds(-10));
            Response.Cache.SetValidUntilExpires(false);
            Response.Cache.VaryByParams.IgnoreParams = false;
            if (Page.IsPostBack || string.IsNullOrEmpty(Request.QueryString["action"]))
            {
                return;
            }
            const string setApplicationRestartedScript = "if (window.parent && window.parent.setApplicationRestarted) { window.parent.setApplicationRestarted(); }";
            _updater = new Updater();
            switch (Request.QueryString["action"])
            {
                case "RestartApplication":
                    Page.ClientScript.RegisterStartupScript(GetType(), "load", setApplicationRestartedScript, true);
                    break;
                case "RemoveHttpModules":
                    _updater.RemoveHttpModules();
                    break;
                case "ApplyConfigPatches":
                    _updater.ApplyConfigPatches();
                    break;
                case "UnzipUpdatePackage":
                    _updater.Unzip();
                    Page.ClientScript.RegisterStartupScript(GetType(), "load", setApplicationRestartedScript, true);
                    break;
                case "Compiling":
                    break;
                case "ApplyDatabasePatches":
                    _updater.ApplyDatabasePatches();
                    Page.ClientScript.RegisterStartupScript(GetType(), "load", setApplicationRestartedScript, true);
                    break;
                case "RestoreHttpModules":
                    _updater.RestoreHttpModules();
                    break;
            }
        }
    }
}